package com.simple.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dfe.modules.sys.dao.SysRoleMenuDao;
import com.dfe.modules.sys.entity.SysRoleMenuEntity;
import com.dfe.modules.sys.service.SysRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;


/**
 * 角色与菜单对应关系
 *
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuDao, SysRoleMenuEntity> implements SysRoleMenuService {

	@Override
	public void saveOrUpdate(Integer roleId, List<Integer> menuIdList) {
		//先删除角色与菜单关系
		deleteByRoleIdList(Arrays.asList(roleId));

		if(menuIdList.size() == 0){
			return ;
		}

		//保存角色与菜单关系
		for(Integer menuId : menuIdList){
			SysRoleMenuEntity sysRoleMenuEntity = new SysRoleMenuEntity();
			sysRoleMenuEntity.setMenuId(menuId);
			sysRoleMenuEntity.setRoleId(roleId);

			save(sysRoleMenuEntity);
		}
	}

	@Override
	public List<Integer> queryMenuIdList(Integer roleId) {
		return baseMapper.queryMenuIdList(roleId);
	}

	@Override
	public void deleteByRoleIdList(List<Integer> roleIdList) {
		QueryWrapper<SysRoleMenuEntity> qw = new QueryWrapper<SysRoleMenuEntity>()
				.in(roleIdList != null && !roleIdList.isEmpty(),"role_id",roleIdList);
		remove(qw);
	}

	@Override
	public void deleteByMenuId(Integer menuId) {
		QueryWrapper<SysRoleMenuEntity> qw = new QueryWrapper<SysRoleMenuEntity>()
				.eq("menu_id",menuId);
		remove(qw);
	}

}
